/**
 * 1. 条件类型：Exclude & Extract
 **/
// 手动实现 Exclude：从 T 中排除 U 的联合类型
type MyExclude<T, U> = T extends U ? never : T;

// 手动实现 Extract：从 T 中提取可分配给 U 的类型
type MyExtract<T, U> = T extends U ? T : never;

// 示例
type A = MyExclude<"a" | "b" | "c", "a" | "b">; // "c"
type B = MyExtract<"a" | "b" | "c", "a" | "d">; // "a"




/**
 * 2. 对象属性操作：Pick & Omit
 */
// 手动实现 Pick：选取对象的指定属性
type MyPick<T, K extends keyof T> = {
    [P in K]: T[P];
};

// 手动实现 Omit：排除对象的指定属性
type MyOmit<T, K extends keyof T> = MyPick<T, Exclude<keyof T, K>>;

// 示例
interface User {
    id: number;
    name: string;
    age: number;
}

type UserPick = MyPick<User, "id" | "name">; // { id: number; name: string }
type UserOmit = MyOmit<User, "age">; // { id: number; name: string }





/**
 * 3. 参数与返回值操作：Parameters & ReturnType
 */
// 手动实现 Parameters：获取函数的参数类型
type MyParameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;

// 手动实现 ReturnType：获取函数的返回值类型
type MyReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : never;

// 示例
const add1 = (a: number, b: number) => a + b;
type AddParams = MyParameters<typeof add1>; // [a: number, b: number]
type AddReturn = MyReturnType<typeof add1>; // number



/**
 * 4. 递归类型：DeepReadonly
 */
type DeepReadonly<T> = {
    readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P];
};

// 示例
interface DeepObj {
    a: { b: number };
    c: string;
}
type ReadonlyDeep = DeepReadonly<DeepObj>; // 所有属性变为只读，嵌套对象递归只读




/**
 * 5. 联合类型分发：Conditional Types
 */
// 示例：实现一个类型，将联合类型中的每个元素映射为新类型
type MapToPromise<T> = T extends any ? Promise<T> : never;
type Result = MapToPromise<"a" | "b">; // Promise<"a"> | Promise<"b">



/**
 * 6. 类型推断：infer
 */
// 手动实现 Awaited：解析 Promise 或嵌套 Promise
type MyAwaited<T> = T extends Promise<infer U> ? MyAwaited<U> : T;

// 示例
type PromiseType = MyAwaited<Promise<Promise<string>>>; // string





/**
 * 7. 复杂对象重构：Overwrite
 */
type Overwrite<T, U> = {
    [K in keyof T]: K extends keyof U ? U[K] : T[K];
} & U;

// 示例
interface Base { a: number; b: string }
interface Override { a: string; c: boolean }
type Result2 = Overwrite<Base, Override>; // { a: string; b: string; c: boolean }




/**
 * 8. 链式操作：Chainable
 */
type Chainable<T = {}> = {
    option<K extends string, V>(key: K, value: V): Chainable<T & { [P in K]: V }>
    get(): T;
};

// 示例
declare const config: Chainable;
const result = config
    .option("width", 100)
    .option("height", 200)
    .get(); // { width: number; height: number }




/**
 * 9. Partial 升级版：DeepPartial
 */
type DeepPartial<T> = {
    [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

// 示例
interface User {
    name: string;
    address: {
        city: string;
        zip: number;
    };
}

type PartialUser = DeepPartial<User>;
// { name?: string; address?: { city?: string; zip?: number } }





/**
 * 10. 只读属性过滤：ReadonlyKeys
 */
type ReadonlyKeys<T> = {
    [P in keyof T]-?: (<U>() => U extends { [Q in P]: T[P] } ? 1 : 2) extends
    (<U>() => U extends { readonly [Q in P]: T[P] } ? 1 : 2) ? P : never;
}[keyof T];

// 简化版（利用 Readonly 约束）
type ReadonlyKeysSimple<T> = {
    [P in keyof T]-?: (T extends { readonly [Q in P]: T[P] } ? P : never);
}[keyof T];

// 示例
interface Foo {
    readonly a: number;
    b: string;
}

type ReadonlyProps = ReadonlyKeys<Foo>; // "a"





/**
 * 11. 构造函数类型：ConstructorParameters
 */
type MyConstructorParameters<T extends new (...args: any) => any> =
    T extends new (...args: infer P) => any ? P : never;

// 示例
class Bar {
    constructor(public x: number, public y: string) { }
}

type BarParams = MyConstructorParameters<typeof Bar>; // [x: number, y: string]








/**
 * 12. 元组转联合类型：TupleToUnion
 */
type TupleToUnion<T extends readonly any[]> = T[number];

// 示例
type Tuple = ["a", "b", "c"];
type Union = TupleToUnion<Tuple>; // "a" | "b" | "c"







/**
 * 13. 联合类型转交集：UnionToIntersection
 */
type UnionToIntersection<U> =
    (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;

// 示例
type Union2 = { a: 1 } | { b: 2 };
type Intersection = UnionToIntersection<Union2>; // { a: 1 } & { b: 2 }




// /**
//  * 14. 类型安全的合并：Merge
//  */
// type Merge<T, U> = {
//     [P in keyof T | keyof U]:
//     P extends keyof U ? U[P] :
//     P extends keyof T ? T[P] : never;
// };

// // 深度合并版本
// type DeepMerge<T, U> = {
//     [P in keyof T | keyof U]:
//     P extends keyof U ?
//     (T[P] extends object ? DeepMerge<T[P], U[P]> : U[P]) :
//     P extends keyof T ? T[P] : never;
// };

// // 示例
// type A2 = { a: 1; b: { c: 2 } };
// type B2 = { a: 3; b: { d: 4 } };
// type Merged = DeepMerge<A2, B2>; // { a: 3; b: { c: 2; d: 4 } }






// /**
//  * 15. 函数重载模拟：Overload
//  */
// type Overload<T> = {
//   <U>(arg: U): U extends string ? number : U extends number ? string : never;
// };

// const overload: Overload<any> = (arg) => {
//   return typeof arg === "string" ? arg.length : arg.toString();
// };

// // 示例
// const num = overload("hello"); // number
// const str = overload(123);     // string





/**
 * 16. 类型守卫：IsEqual
 */
type IsEqual<T, U> = 
  (<G>() => G extends T ? 1 : 2) extends 
  (<G>() => G extends U ? 1 : 2) ? true : false;

// 示例
type A3 = { a: 1 };
type B3 = { a: 1 };
type C3 = { a: 2 };

type Test1 = IsEqual<A3, B3>; // true
type Test2 = IsEqual<A3, C3>; // false






/**
 * 17. 可选链类型：OptionalChain
 */
type OptionalChain<T, K extends string> = 
  K extends `${infer Head}.${infer Tail}` ? 
    Head extends keyof T ? OptionalChain<T[Head], Tail> : never :
  K extends keyof T ? T[K] : never;

// 示例
interface User2 {
  name: {
    first: string;
    last?: string;
  };
}

type LastName = OptionalChain<User2, "name.last">; // string | undefined







/**
 * 18. 模式匹配：If
 */
type If<C extends boolean, T, F> = C extends true ? T : F;

// 示例
type IsString = If<string extends "hello" ? true : false, "yes", "no">; // "no"